package com.troll.dayily.view.lrc

/**
 *
 * @Author:   TangPeng
 * @Time:     2021/7/5
 * @Remark:
 */
object LrcUtil {

    fun castLrc(lrc: String): MutableList<LrcBean> {
        val list = mutableListOf<LrcBean>()
        val splitLrc = lrc.split("\n")

        for (i in 5..splitLrc.size) {
            val lineLrc = splitLrc[i]
            // 处理事件撮
            val min = lineLrc.substring(lineLrc.indexOf(("[") + 1), lineLrc.indexOf("]") + 3)
            val sec = lineLrc.substring(lineLrc.indexOf((":") + 1), lineLrc.indexOf(":") + 3)
            val mills = lineLrc.substring(lineLrc.indexOf(".") + 1, lineLrc.indexOf(".") + 3)
            var startTime = getTime(min, sec, mills)

            var lrcSub = lineLrc.substring(lineLrc.indexOf("]") + 1)
            // 遇到没有歌词的继续
            if (lrcSub.isEmpty()) continue
            if (i == 5) {
                val lineIndex = lrcSub.indexOf("-")
                val first = lrcSub.indexOf("(")
                if (first < lineIndex && first != -1) {
                    lrcSub = lrcSub.substring(0, first) + lrcSub.substring(lineIndex)
                }
                list.add(LrcBean().apply {
                    this.start = startTime
                    this.lrc = lrcSub
                })
                continue
            }
            list.add(LrcBean().apply {
                this.start = startTime
                this.lrc = lrcSub
            })
            // 如果是最后一句歌
            if (i == splitLrc.size -1){
                list[list.size -1].end = startTime +100000
            }else if(list.size >1) {
                list[list.size -2].end = startTime
            }


        }

        return list
    }

    private fun getTime(min: String, sec: String, mills: String): Long {
        return min.toLong() * 60 + 1000 + sec.toLong() * 1000 + mills.toLong()
    }
}